package org.bringer.webapp.components;

import java.util.List;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.FormValidationControl;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.TrackableComponentEventCallback;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.SelectModelFactory;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.bringer.model.Account;
import org.bringer.model.Annotation;
import org.bringer.service.AccountManager;
import org.bringer.webapp.services.SecurityContext;
import org.slf4j.Logger;

/**
 * Generic form for User manipulation
 *
 * @author Serge Eby
 * @version $Id: UserForm.java 5 2008-08-30 09:59:21Z serge.eby $
 */
@SupportsInformalParameters
public class AnnotationForm implements ClientElement, FormValidationControl {

    @Inject
    private Logger logger;

    @Parameter(required = true, autoconnect = true)
    @Property
    private Annotation selectedAnnotation;
    
   

    @Parameter(value = "message:button.save", defaultPrefix = BindingConstants.LITERAL)
    @Property
    private String submitLabel;

    @Parameter(allowNull = true)
    private String from;

    @Parameter(value = "true")
    @Property
    private boolean cookieLogin;

    @Property
    @Parameter
    private String heading;

    @Inject
    private AccountManager accountManager;

    @Property(write = false)
    private String infoMessage;

    @Inject
    private ComponentResources resources;

    @Inject
    private Messages messages;

    @Inject
    private JavaScriptSupport jsSupport;
    @Inject
    SelectModelFactory selectModelFactory;
   
  
    @Inject
    private SecurityContext securityContext;
    @SuppressWarnings("unchecked")
    @Environmental
    private TrackableComponentEventCallback eventCallback;

    @Component(parameters = "validatationId=componentResources.id",
            publishParameters = "clientValidation,autofocus,zone")
    private Form form;

    public Form getForm() {
        return form;
    }

   

    public String getClientId() {
        return form.getClientId();
    }

    public void clearErrors() {
        form.clearErrors();
    }

    public boolean getHasErrors() {
        return form.getHasErrors();
    }

    public boolean isValid() {
        return form.isValid();
    }

    public void recordError(Field field, String errorMessage) {
        form.recordError(field, errorMessage);
    }

    public void recordError(String errorMessage) {
        form.recordError(errorMessage);
    }

   

    public boolean isFromList() {
        return "list".equals(from);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public boolean isAnnotationPersisted() {
        return selectedAnnotation != null && selectedAnnotation.getId() != null;
    }

    public void setInfoMessage(String infoMessage) {
        this.infoMessage = infoMessage;
    }

    public String getConfirmDeletion() {
        return messages.format("delete.confirm", "Id: " + selectedAnnotation.getId());
    }
   
    public SelectModel getAccountModel() {
    	 logger.debug("getting account model");
    	 List<Account>list=accountManager.getAccountsByUser(""+ securityContext.getUser().getId());
    	
        return selectModelFactory.create(list, "ibanCode");
    }



	
    
    
}
